/**
 * 市场数据 API 客户端
 */

const API_BASE_URL = import.meta.env.VITE_API_URL || "http://localhost:4000";

export interface StockQuote {
  symbol: string;
  name: string;
  lastPrice: number;
  openPrice: number;
  highPrice: number;
  lowPrice: number;
  volume: number;
  change: number;
  changePercent: number;
  timestamp: string;
}

export interface PriceHistoryPoint {
  timestamp: string;
  price: number;
  volume: number;
}

/**
 * 获取所有股票行情
 */
export async function getAllMarketQuotes(): Promise<StockQuote[]> {
  const response = await fetch(`${API_BASE_URL}/api/market/quotes`);
  if (!response.ok) {
    throw new Error("获取市场行情失败");
  }
  const data = await response.json();
  return data.quotes;
}

/**
 * 获取指定股票的行情
 */
export async function getMarketQuote(symbol: string): Promise<StockQuote> {
  const response = await fetch(`${API_BASE_URL}/api/market/quote/${symbol}`);
  if (!response.ok) {
    throw new Error(`获取 ${symbol} 行情失败`);
  }
  const data = await response.json();
  return data.quote;
}

/**
 * 获取股票价格历史
 */
export async function getPriceHistory(
  symbol: string,
  limit: number = 100,
): Promise<PriceHistoryPoint[]> {
  const response = await fetch(
    `${API_BASE_URL}/api/market/history/${symbol}?limit=${limit}`,
  );
  if (!response.ok) {
    throw new Error(`获取 ${symbol} 价格历史失败`);
  }
  const data = await response.json();
  return data.history;
}




